/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PodnosenjeZahtjevaKMPanel.java
 *
 * Created on Nov 24, 2010, 5:19:35 PM
 */
package hraplication;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Nermin
 */
public class PregledZahtjevaKMPanel extends Paneli {
    ArrayList<String> lista;
    Konekcija kon;
    int idUposlenika;

    /** Creates new form PodnosenjeZahtjevaKMPanel */
    public PregledZahtjevaKMPanel(Konekcija konekcija) {
        initComponents();
        lista = new ArrayList<String>(20);
        kon = konekcija;
        OsvjeziChBOX();
        idUposlenika = 0;
        
    }

    public void setIDuposlenika(int IDuposlenika){
        idUposlenika = IDuposlenika;
    }
     public void OsvjeziChBOX(){
        lista.clear();
        NarudzbaCombo.removeAllItems();
        ResultSet r = kon.Select("SELECT idTrebovanje, datum_podnosenja FROM podneseni_zahtjevi_za_trebovanje "+
                "WHERE Uposlenici_idUposlenici = "+idUposlenika);
        if (r!=null){
            try{
                while (r.next()){
                    NarudzbaCombo.addItem(r.getInt(1));
                    lista.add(r.getDate(2).toString());
                }
            }
            catch (SQLException ex)
            {
              System.err.println(ex.getMessage());
            }
        }
               
        OsvjeziTabelu();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tabelaPanel = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        NarudzbaCombo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Zahtjev za kancelarijskim materijalom"));
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hraplication.HRaplicationApp.class).getContext().getResourceMap(PregledZahtjevaKMPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        tabelaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Odabrani materijal"));
        tabelaPanel.setName("tabelaPanel"); // NOI18N

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Naziv", "Kolicina"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setName("tabela"); // NOI18N
        tabela.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tabelaKeyReleased(evt);
            }
        });
        tabelaPanel.setViewportView(tabela);

        NarudzbaCombo.setName("NarudzbaCombo"); // NOI18N
        NarudzbaCombo.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                NarudzbaComboPropertyChange(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabelaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NarudzbaCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(NarudzbaCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(37, 37, 37)
                .addComponent(tabelaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tabelaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabelaKeyReleased

        if (evt.getKeyCode() == evt.VK_ESCAPE){
            tabela.clearSelection();
        }
    }//GEN-LAST:event_tabelaKeyReleased

    private void NarudzbaComboPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_NarudzbaComboPropertyChange
        OsvjeziTabelu();
    }//GEN-LAST:event_NarudzbaComboPropertyChange

    private void OsvjeziTabelu() {
        if (NarudzbaCombo.getSelectedIndex()>=0)
            jLabel3.setText(lista.get(NarudzbaCombo.getSelectedIndex()).toString());

        DefaultTableModel m = (DefaultTableModel) tabela.getModel();
        m.getDataVector().clear();

        String upit = "SELECT DISTINCT m.naziv, t.trebovana_kolicina " +
"FROM trebovani_materijali AS t, materijali AS m, podneseni_zahtjevi_za_trebovanje AS zah " +
        "WHERE t.idTrebovanje = " + NarudzbaCombo.getSelectedItem() +
                " AND t.Materijali_idMaterijali = m.idMaterijali";

        
        ResultSet r = kon.Select(upit);
        if (r!=null){
            try{
                while (r.next()){
                    m.addRow(new Object[] {r.getString(1), r.getInt(2)});
                }
            }
            catch (SQLException ex)
            {
              System.err.println(ex.getMessage());
            }
        }

        tabela.repaint();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox NarudzbaCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTable tabela;
    private javax.swing.JScrollPane tabelaPanel;
    // End of variables declaration//GEN-END:variables

   
}
